package com.hotai.controller;


import cn.hutool.core.lang.UUID;
import com.aliyun.oss.OSS;
import com.hotai.comment.dtos.ResponseResult;
import com.hotai.comment.enums.AppHttpCodeEnum;
import com.hotai.comment.fastDFS.AliyunConfig;
import com.hotai.comment.fastDFS.MultipartFileToFileUtil;
import com.hotai.comment.pojo.Courseware;
import com.hotai.service.CoursewareService;
import com.hotai.vo.courseware.CoursewareList;
import com.hotai.vo.courseware.CoursewareSave;
import com.hotai.vo.courseware.CoursewaredetaileList;
import com.hotai.vo.courseware.Coursewarejia;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;

@RestController
@RequestMapping("/courseware")
@CrossOrigin
public class CoursewareContrller {

    @Autowired
    private CoursewareService coursewareService;

    @Autowired
    private   AliyunConfig aliyunConfig;


    //管理端 课件首页
    @RequestMapping("/list")
    private ResponseResult  CoursewareLit(CoursewareList  query){
        return    coursewareService.PageList(query);
    }
//管理端 可见详情
    @RequestMapping("/detail")
    private ResponseResult  Coursewaredetail(CoursewaredetaileList queryware){
        return    coursewareService.Waredetail(queryware);
    }

//图片上传
    @RequestMapping("/wareimg")
    private ResponseResult  Coursewareimg(@RequestParam("file") MultipartFile file){
        return    coursewareService.CourseImg(file);
    }

//文件上传
    @RequestMapping("/warejian")
    private ResponseResult  Coursewarejian(@RequestParam("file") MultipartFile file){
        return    coursewareService.Coursewarejian(file);
    }


    /**
     * 上传文件（阿里OSS）
     *
     * @param file 文件对象
     * @return
     */
//    @PostMapping("/uploadFile")
    @ResponseBody
    public ResponseResult uploadFile(@RequestParam(name = "file") MultipartFile file) {
        String saveUrl = "";
        try {
//            log.info("------文件上传方法执行------");
            long startTime = System.currentTimeMillis();
            // 阿里OSS
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            Date date = new Date();
            // 阿里云API的文件夹名称（子文件夹）
            String format = dateFormat.format(date) + "/";
            // 文件名
            String formats = file.getName();
            // MultipartFile 转 File
            File newFile = MultipartFileToFileUtil.multipartFileToFile(file);
            // 返回结果 resultArr[0]：唯一MD5数字签名  resultArr[1]：如："appversion/20200723/a3662009-897c-43ea-a6d8-466ab8310c6b.apk"
            String[] resultArr = coursewareService.uploadObject2OSS(aliyunConfig.ossClient(), newFile, aliyunConfig.getBucketName(), "keApk/", format, formats);
            // 网络URL地址：s端传入cdn链接 + resultArr[1]
            if (resultArr != null) {
                String path = resultArr[1];
                // 保存路径url地址：http://zhiliaoappversion.oss-cn-beijing.aliyuncs.com/appversion/20200723/a3662009-897c-43ea-a6d8-466ab8310c6b.apk
                saveUrl = aliyunConfig.getUrl(aliyunConfig.getBucketName(), path);
            }

            // 删除本地临时文件
            MultipartFileToFileUtil.deleteTempFile(newFile);
            long endTime = System.currentTimeMillis();
//            log.info("------文件上传成功，耗时" + ((endTime - startTime) / 1000) + "s------");
            return  ResponseResult.okResult(saveUrl);
        } catch (Exception e) {
            e.printStackTrace();
//            log.info("------文件上传失败------");
            return ResponseResult.errorResult(505,"上传失败");
        }
    }



    //版本更新
    @RequestMapping("/renewal")
    private ResponseResult  renewal(@RequestParam("file") MultipartFile file){
        return    coursewareService.Benrenewal(file);
    }


    //文件下载
    @RequestMapping("/xiaZai")
    private ResponseResult   Xiajian(@Param("id") Integer id){
        return    coursewareService.xiaZai(id);
    }


        //课件 添加
    @RequestMapping("/save")
    private ResponseResult  Courseware (@RequestBody CoursewareSave queryware){
        return    coursewareService.Savedetail(queryware);
    }



    //管理端  修改 回显
    @RequestMapping("/findBYid")
    private ResponseResult  CoursewareFinbyid(@Param("id") Integer id){
        return    coursewareService.CourseFinbyid(id);
    }

    //上下架
    @RequestMapping("/jia")
    private ResponseResult  sxjia(@RequestBody  Coursewarejia coursewarejia){
        return    coursewareService.CourseJia(coursewarejia);
    }

    //全上架
    @RequestMapping("/Shangjia")
    private ResponseResult  Shangjia(@Param("ide")  String  ide){
        return    coursewareService.ShangJia(ide);
    }

    //全下架
    @RequestMapping("/Xiajia")
    private ResponseResult  Xiajia(@Param("ide")  String  ide){
        return    coursewareService.XiaJia(ide);
    }


    //课件详情里面卸载
    @RequestMapping("/deleteShe")
    private ResponseResult  deleteShe(@Param("cid") Integer cid,@Param("eid") Integer   eid){
        return    coursewareService.deleteIdShe(cid,eid);
    }

    //删除
    @RequestMapping("/delete")
    private ResponseResult  delete(@Param("id") Integer id){
        return    coursewareService.deleteId(id);
    }

    //全选删除
    @RequestMapping("/deleteAll")
    private ResponseResult  deleteA(@Param("ide")  String  ide){
        return    coursewareService.deleteIAll(ide);
    }
}
